import { Injectable, OnInit } from '@angular/core';
import { HttpClient, HttpParams, HttpHeaders } from '@angular/common/http';
import { AppSettings } from '../../shared/config/app-settings';
import { Observable } from 'rxjs/internal/Observable';
import { map } from 'rxjs/operators';
import { CommonResp, Propertylist } from '../interface';
import * as _ from 'lodash';
@Injectable({
  providedIn: 'root'
})
export class SettingsService implements OnInit {

  constructor(
    private httpClient: HttpClient,
  ) {
    // TODO
  }

  ngOnInit(): void {
    // TODO
  }

  getAll(filter?): Observable<any> {
    let header = _.cloneDeep(AppSettings.httpServerHeader);
    if (filter) {
      header.params = filter;
    }
    return this.httpClient.get<Array<any>>(`${AppSettings.server_base}/feature`, header).pipe(map(res => res['success'] ? res['data'] : []));
  }

  insert(entity): Observable<any> {
    let header = _.cloneDeep(AppSettings.httpServerHeader);
    return this.httpClient.post(`${AppSettings.server_base}/feature`, entity, header);
  }

  update(entity): Observable<any> {
    if(entity.id <= 0){
      console.error('update entity id should large than 1');
      return;
    }
    let header = _.cloneDeep(AppSettings.httpServerHeader);
    return this.httpClient.post(`${AppSettings.server_base}/feature/${entity.id}`, entity, header);
  }

  delete( ids:any[]) :Observable<any> {
    let header = _.cloneDeep(AppSettings.httpServerHeader);
    header.params = new HttpParams();
    header.params = header.params.set('id', ids.join(','));
    return this.httpClient.delete(`${AppSettings.server_base}/feature`, header);
  }

}